﻿using System.Drawing;

namespace Katamino
{
    /// <summary>
    /// Everything that has to do with Rendering the Board
    /// </summary>
    /// 
    public class BoardWithGraphics : Board
    {
        public BoardWithGraphics(int w, int h)
            : base(w, h)
        {
            hasGraphics = true;
            InitializeGraphics();
        }

        // static 'class' variables, that I only need once.
        public static bool hasGraphics = false;
        public static Color[] palette = { Color.LightGray,
                                      Color.Blue, Color.Red, Color.Green, Color.Brown, Color.Purple, Color.Orange,
                                      Color.LightBlue, Color.Pink, Color.LightGreen, Color.Yellow, Color.Violet, Color.Lime,
                                      Color.WhiteSmoke};

        // instance variables.
        public Bitmap picture = null;
        public Graphics graphics = null;

        public void InitializeGraphics()
        {
            picture = new Bitmap(1, 1);
            graphics = Graphics.FromImage(picture);
            graphics.Clear(Color.White);
        }

        public void InitializeGraphics(int width, int height)
        {
            picture = new Bitmap(width, height);
            graphics = Graphics.FromImage(picture);
            graphics.Clear(Color.White);
        }

        public void CloneGraphics(BoardWithGraphics b)
        {
            picture = (Bitmap)b.picture.Clone();
            graphics = Graphics.FromImage(picture);
        }

        public Bitmap Render(int width, int height)
        {
            var bw = Width; // Board sizes
            var bh = Height;
            var bmw = width - 1; // bitmap sizes, but allows border
            var bmh = height - 1;
            var sqw = bmw / bw; // Aspect ratio?
            var sqh = bmh / bh;
            var sq = (sqw < sqh ? sqw : sqh); // Determine size of square that looks square and not rectangle

            InitializeGraphics(width, height);

            // var filler = new SolidBrush(Color.LightGray);
            var border = new Pen(Color.Gray);

            for (int iw = 0; iw < bw; iw++)
                for (int ih = 0; ih < bh; ih++)
                {
                    var filler = new SolidBrush(palette[D[iw, ih]]);
                    // Color.LightGray);

                    graphics.FillRectangle(filler, iw * sq, ih * sq, sq - 1, sq - 1); // -1 for shadowing effect
                    graphics.DrawRectangle(border, iw * sq, ih * sq, sq, sq);
                }

            return (picture);
        }

        public BoardWithGraphics Add(Penta p, int number)
        {

            BoardWithGraphics result = new BoardWithGraphics(this.Width, this.Height);
            result.CloneGraphics(this);

            Graphics graph = Graphics.FromImage(result.picture);
            Brush b = new SolidBrush(palette[number]);

//            for (int ip = 0; ip < p.x.Length && ip < p.y.Length; ip++)
            {
//                result.D[p.x[ip] + fw, p.y[ip] + fh] = number;
                // TODO: change this to fill picturebox a bit better
                // graph.FillRectangle(b, 1 + 16 * (p.y[ip] + fh), 1 + 16 * (p.x[ip] + fw), 15, 15);
            }
            result.firstFree(); // update the first free field

            return result;
        }
    }
}
